package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.EmployeeLoanListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.EmployeeLoanContent;
import com.isunland.managesystem.entity.EmployeeLoanOriginal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EmployeeLoanListFragment extends BaseListFragment {
  /** 添加借款 */
  private static final int ADD_EMPLOYEELOAN = 0;
  /** 借款详情 */
  private static final int EMPLOYEELOAN_DETAIL = 1;
  /** 差旅 */
  private static final int TRAVERERMBURSE_DETAIL = 2;
  /** 管理 */
  private static final int MANAGE_DETAIL = 3;

  private EmployeeLoanListAdapter mAdapter;
  private ArrayList<EmployeeLoanContent> mEmployeeLoanContents;// 解析列表集合

  // 列表url
  @Override public String getUrl() {
    return ApiConst.URL_Person_Expense;
  }

  // 获取借款列表的输入参数
  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("curPage", 1 + "");
    params.put("pageSize", 20 + "");
    params.put("applicantId", mCurrentUser.getJobNumber());
    return params;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.personalList);
    super.onCreate(savedInstanceState);
  }

  // 创建菜单
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:

        FragmentManager fm = getActivity().getSupportFragmentManager();
        DialogFragment dialog = new CertificateChooseDialogFragment();// 选择申请类型dialog
        dialog.setTargetFragment(EmployeeLoanListFragment.this, ADD_EMPLOYEELOAN);
        dialog.show(fm, "");

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  // 解析
  @Override public void resolveResponse(String arg0) {
    EmployeeLoanOriginal original = new Gson().fromJson(arg0, EmployeeLoanOriginal.class);
    List<EmployeeLoanContent> content = original.getRows();
    setAdapter(content);
  }

  private void setAdapter(List<EmployeeLoanContent> content) {
    if (mEmployeeLoanContents == null) {
      mEmployeeLoanContents = new ArrayList<>();
    }
    // 不为空
    mEmployeeLoanContents.clear();
    mEmployeeLoanContents.addAll(content);
    if (mAdapter == null) {
      mAdapter = new EmployeeLoanListAdapter(mActivity, mEmployeeLoanContents);
      setListAdapter(mAdapter);
    }
    ((EmployeeLoanListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  public void refresh() {
    volleyPost();
  }

  // 列表点击item
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    EmployeeLoanContent mContent = mEmployeeLoanContents.get(position - 1);
    String certificateCode = mContent.getCertificateCode();
    /*
		 * "差旅费用报销": "100014-DJLB-CWZC-CLFYBX", 员工借款支出:
		 * "100014-DJLB-CWZC-YGJKZC", 还款收入: "100014-DJLB-CWSR-HKSR", 管理费用报销:
		 * "100014-DJLB-CWZC-GLFYBX"
		 */
    // 员工借款支出详情参数
    StringBuilder code1 = new StringBuilder();
    code1.append(mCurrentUser.getMemberCode()).append("-").append("DJLB-CWZC-YGJKZC");
    // 差旅费用报销详情参数
    StringBuilder code2 = new StringBuilder();
    code2.append(mCurrentUser.getMemberCode()).append("-").append("DJLB-CWZC-CLFYBX");
    // 管理费用报销详情参数
    StringBuilder code3 = new StringBuilder();
    code3.append(mCurrentUser.getMemberCode()).append("-").append("DJLB-CWZC-GLFYBX");
    // 还款收入详情参数
    StringBuilder code4 = new StringBuilder();
    code4.append(mCurrentUser.getMemberCode()).append("-").append("DJLB-CWSR-HKSR");
    // 员工借款
    if (certificateCode.equals(code1.toString())) {
      Intent i1 = new Intent(getActivity(), AddEmpoyeeLoanActivity.class);
      i1.putExtra(EmpoyeeLoanDetailFragment.EXTRA_EMPLOYEELOANContent, mContent);
      i1.putExtra(AddEmpoyeeLoanActivity.EXTRA_TYPE, AddEmpoyeeLoanActivity.EXTRA_SHOWCONTENTTYPE);
      startActivityForResult(i1, EMPLOYEELOAN_DETAIL);
    }
    // 差旅费用报销
    else if (certificateCode.equals(code2.toString())) {
      Intent i2 = new Intent(getActivity(), TravelReimburseActivity.class);
      i2.putExtra(TravelReimburseDetailFragment.EXTRA_TRAVELREIMBURSE_CONTENT, mContent);
      startActivityForResult(i2, TRAVERERMBURSE_DETAIL);
    }
    // 管理费用报销
    else if (certificateCode.equals(code3.toString())) {
      Intent i3 = new Intent(getActivity(), ManagelReimburseActivity.class);
      i3.putExtra(ManagelReimburseDetailFragment.EXTRA_MANAGEREIMBURSE_CONTENT, mContent);
      startActivityForResult(i3, MANAGE_DETAIL);
    }
    // 还款收入
    else if (certificateCode.equals(code4.toString())) {
      Intent i4 = new Intent(getActivity(), EmployeePaymentActivity.class);
      i4.putExtra(ShowEmployeePaymentFragment.EXTRA_EMPLOYEELOANContent, mContent);
      i4.putExtra(EmployeePaymentActivity.EXTRA_TYPE,
          EmployeePaymentActivity.EXTRA_SHOWCONTENTTYPE);
      startActivityForResult(i4, MANAGE_DETAIL);
    }
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case ADD_EMPLOYEELOAN:
        volleyPost();
        break;
      case EMPLOYEELOAN_DETAIL:
        volleyPost();

        break;
      // 管理
      case MANAGE_DETAIL:
        volleyPost();
        break;

      case TRAVERERMBURSE_DETAIL:
        volleyPost();
        break;

      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}